package com.molyfun.parents.zhibo.playback;

import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:20/04/2018.
 *
 * 消息分发队列
 *
 * 根据传入的时间获取消息列表
 *
 *
 */

public class PlayBackMsgDispatcher extends LinkedList<JsonObject> {

    private static final int MAX_CACHE_SIZE = 300;

    private static final int CACHE_DEL_SIZE = 60;

    private OnLoadMoreDataListener mOnLoadMoreDataListener;

    //游标
    private int position = 0;

    public void addAll(List<JsonObject> jsonObjects){
        if(jsonObjects != null && jsonObjects.size() > 0){
            if(size() == 0){
                addAll(0, jsonObjects);
            }else{
                //判断时间范围

                JsonObject firstJsonObject = get(0);
                int firstStamp = firstJsonObject.get("timelenstamp").getAsInt();
                JsonObject lastJsonObject = get(size() - 1);
                int lastStamp = lastJsonObject.get("timelenstamp").getAsInt();

                JsonObject addDataFirstJsonObject = jsonObjects.get(0);
                int addFirstStamp = addDataFirstJsonObject.get("timelenstamp").getAsInt();
//                JsonObject addDataLastJsonObject = jsonObjects.get(size() - 1);
//                int addLastStamp = addDataLastJsonObject.get("timelenstamp").getAsInt();

                if(addFirstStamp <= firstStamp){
                    clear();
                    addAll(0, jsonObjects);
                }else{
                    if(addFirstStamp >= lastStamp){
                        addAll(size(), jsonObjects);
                    }else{
                        //中间范围,需要截取
                        int splitNum = 0;
                        for(JsonObject jsonObject : this){
                            int timeStamp = jsonObject.get("timelenstamp").getAsInt();
                            if(timeStamp >= addFirstStamp){
                                this.removeRange(splitNum, size() - 1);
                                addAll(splitNum, jsonObjects);
                                break;
                            }
                            splitNum ++;
                        }

                    }
                }

            }

        }else{
            return;
        }
    }

    //获取某一个时间点的数据
    public List<JsonObject> getDataBySeek(long timePosition){
        System.out.println("getData--->timePosition--->" + timePosition + "size" + size());
        if(size() > 0){
            ArrayList<JsonObject> jsonObjects = new ArrayList<>();
            for(int currentPosition = 0; currentPosition < size(); currentPosition ++){
                //判断一条数据的时间
                JsonObject jsonObject = get(currentPosition);
                long timeStamp = jsonObject.get("timelenstamp").getAsLong();

                if((size() - currentPosition) < 3){
                    if(mOnLoadMoreDataListener != null){
                        mOnLoadMoreDataListener.onLoadMoreMsg();
                    }
                }

                if(timeStamp <= timePosition){
                    jsonObjects.add(jsonObject);
                }else{
                    position = currentPosition;
                    break;
                }
            }
            //
            if(size() > MAX_CACHE_SIZE && position > (MAX_CACHE_SIZE - CACHE_DEL_SIZE)){
                removeRange(0, CACHE_DEL_SIZE);
            }
            return jsonObjects;
        }else{
            return null;
        }
    }

    //获取某一个时间点的数据
    public List<JsonObject> getData(long timePosition){
        return getData(timePosition, true);
    }

    //获取某一个时间点的数据
    public List<JsonObject> getData(long timePosition, boolean hasMoreData){
        System.out.println("getData--->timePosition--->" + timePosition + "size" + size());
        if(size() > 0){
            ArrayList<JsonObject> jsonObjects = new ArrayList<>();
           for(int currentPosition = position; currentPosition < size(); currentPosition ++){
               //判断一条数据的时间
               JsonObject jsonObject = get(currentPosition);
               long timeStamp = jsonObject.get("timelenstamp").getAsLong();

               if(((size() - currentPosition) < 3) && hasMoreData){
                   if(mOnLoadMoreDataListener != null){
                       mOnLoadMoreDataListener.onLoadMoreMsg();
                   }
               }

               if(timeStamp <= timePosition){
                   jsonObjects.add(jsonObject);
               }else{
                   position = currentPosition;
                   break;
               }
           }
           //
           if(size() > MAX_CACHE_SIZE && position > (MAX_CACHE_SIZE - CACHE_DEL_SIZE)){
               removeRange(0, CACHE_DEL_SIZE);
           }
           return jsonObjects;
        }else{
            return null;
        }
    }

    public void setOnLoadMoreDataListener(OnLoadMoreDataListener onLoadMoreDataListener){
        this.mOnLoadMoreDataListener = onLoadMoreDataListener;
    }

    public interface OnLoadMoreDataListener{
        void onLoadMoreMsg();
    }

}